import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
#np.random.seed(42)
x1 = 2*np.random.rand(100,1)
x2 = 2*np.random.rand(100,1)
x = np.c_[x1,x2]
y = 4 + 3*x1 + 5*x2 +np.random.rand(100,1)
# LinearRegression()创建一个回归的对象 LinearRegression为回归类
reg = LinearRegression()
# fit方法：输入x,y，拟合线性模型
reg.fit(x,y)
# reg.intercept_截距项b reg.coef_参数θ（w)
print(reg.intercept_,reg.coef_)

"""
# LinearRegression()创建一个回归的对象 LinearRegression为回归类
reg = LinearRegression(fit_intercept=False)
# fit方法：输入x,y，拟合线性模型
reg.fit(x,y)
# reg.intercept_截距项b reg.coef_参数θ（w)
print(reg.intercept_,reg.coef_)
"""


x_new = np.array([[0,0],
                  [2,1],
                  [2,4]])
y_predict = reg.predict(x_new)
print(y_predict)
plt.plot(x_new[:,0],y_predict,'r-')
plt.plot(x1,y,'b.')
plt.axis([0,2,0,30])
plt.show()